package com.lianjinsu.shop.service.impl;

import com.lianjinsu.shop.entity.Shop;
import com.lianjinsu.shop.repository.dao.ShopDao;
import com.lianjinsu.shop.service.ShopService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements ShopService {

    private final ShopDao shopDao;

    @Override
    public boolean insertGoods(Shop shop) {
        return shop.insert();
    }

    @Override
    public boolean removeById(int id) {
        return shopDao.removeById(id);
    }

    @Override
    public boolean increaseStock(int id, int stock) {
        return shopDao.increaseStock(id, stock);
    }

    @Override
    public boolean hotOrNot(int id, boolean hotOrNot) {
        return shopDao.hotOrNot(id, hotOrNot);
    }

    @Override
    public List<Shop> searchByType(String type) {
        return shopDao.selectByType(type);
    }

    @Override
    public Set<String> allTags() {
        List<Shop> allList = shopDao.list();
        return allList.stream().map(Shop::getTag).collect(Collectors.toSet());
    }

    @Override
    public List<Shop> selectByTags(Collection<String> tags) {
        return shopDao.selectByTags(tags);
    }

    @Override
    public List<Shop> hotList() {
        return shopDao.hotList();
    }

    @Override
    public List<Shop> shopList(String search) {
        if (StringUtils.isBlank(search)) {
            return shopDao.list();
        }
        return shopDao.selectListName(search);
    }

    @Override
    public Shop shopDetail(int shopId) {
        return shopDao.getById(shopId);
    }
}
